<template>
  <div class="home card">
    <div class="new-main">
      <div class="new-main-card" v-for="(list, key, i) in businessList" :Key="i">
        <div class="new-main-card-title">
          <img v-if="showImg(i, 'blue')" src="./img/blue.png" />
          <img v-if="showImg(i, 'green')" src="./img/green.png" />
          <img v-if="showImg(i, 'orange')" src="./img/orange.png" />
          <img v-if="showImg(i, 'purple')" src="./img/purple.png" />
          <img v-if="showImg(i, 'red')" src="./img/red.png" />
          <span>{{ list[0].module_zh }}</span>
        </div>
        <div class="new-main-card-content">
          <div class="content-item" v-for="(item, j) in list" :Key="j" @click="goToTicketDetail(item)">
            <span>{{ item.name }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { getBusinessListApi } from "@/api/modules/ticket/pending";
import { useRouter } from "vue-router";
// 获取路由实例
const router = useRouter();
const businessList = ref<any>({});

const getBusinessList = async () => {
  let { data } = await getBusinessListApi();
  const list = data.results || [];
  const bList = {};
  list.forEach(item => {
    if (!bList[item.module]) {
      bList[item.module] = [];
    }
    bList[item.module].push(item);
  });
  businessList.value = bList;
};

const goToTicketDetail = data => {
  router.push({
    name: "ticket_detail",
    query: {
      business_id: data.id
    }
  });
};

const showImg = (i, name) => {
  const img = ["blue", "green", "orange", "blue", "purple", "green", "red", "blue", "green", "orange"];
  const index = i % img.length || 0;
  return img[index] === name;
};

onMounted(() => {
  getBusinessList();
});
</script>

<style lang="scss" scoped>
.home {
  width: 100%;
  height: 100%;
  overflow: hidden;
  .new-main {
    width: 100%;
    height: 100%;
    .new-main-card {
      float: left;
      min-width: 300px;
      max-width: 400px;
      padding: 0 32px 32px;
      margin: 0 16px 16px 0;
      background: var(--el-menu-bg-color);
      border-radius: 4px;
      box-shadow: 0 2px 12px var(--el-header-menu-shadow-color);
      .new-main-card-title {
        display: flex;
        padding: 0 0 12px;
        color: var(--el-header-logo-text-color);
        border-bottom: 1px solid var(--el-border-color);
        span {
          margin-top: 25px;
          margin-left: 24px;
          font-size: 18px;
          font-weight: 600;
        }
      }
      .new-main-card-content {
        display: flex;
        flex-direction: column;

        // gap: 16px;
        padding: 16px 0 0;
        border-radius: 4px;
        .content-item {
          padding: 8px 16px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          cursor: pointer;
        }
        .content-item:hover {
          color: var(--el-color-primary);
          background: var(--el-menu-hover-bg-color);
        }
      }
    }
  }
}
</style>
